package com.emrahsari.andengineexample;

import java.util.Random;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;

import android.os.Handler;
import android.os.SystemClock;
import android.widget.Toast;

/**
 * @author Nicolas Gramlich
 * @since 15:13:46 - 15.06.2010
 */
public class TouchDragExample extends BaseExample{

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	
	private static int score = 0;

	private Camera mCamera;
	private Texture mTexture;
	
	 Handler myHandler = new Handler();
	
	void setScore(){
		score++;
		CharSequence text = Integer.toString(score);
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	};
	
	void getScore(){
		CharSequence text = Integer.toString(score);
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	};

	Random rand = new Random();

	
	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mEngine.getTextureManager().loadTexture(this.mTexture);
	}
	

    
	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		final Scene scene = new Scene(1);
		scene.setBackground(new ColorBackground(1f, 1f, 1f));
		

	
		final Rectangle centerRectangle = new Rectangle(rand.nextInt(700), rand.nextInt(460), 50, 50){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()) {
					setScore();
					addFace();
					scene.setBackground(new ColorBackground(0, 0, 0));
					scene.setBackground(new ColorBackground(1f, 1f, 1f));
					return true;
				}
				return false;
			}
		};
		centerRectangle.setColor(0, 0, 0);
		//centerRectangle.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier(new RotationModifier(6, 0, 360), new SequenceEntityModifier(new ScaleModifier(3, 1, 1.5f), new ScaleModifier(3, 1.5f, 1)))));
		scene.getLastChild().attachChild(centerRectangle);
		scene.registerTouchArea(centerRectangle);

		scene.setTouchAreaBindingEnabled(true);
		
		return scene;
	}
	
	private void addFace() {
		final Scene scene = this.mEngine.getScene();
		
		
		final Rectangle centerRectangle = new Rectangle(rand.nextInt(700), rand.nextInt(460), 50 , 50){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()) {
			    	setScore();
			    	scene.setBackground(new ColorBackground(0, 0, 0));
			    	SystemClock.sleep(1000);
			    	addFace();
					scene.setBackground(new ColorBackground(1f, 1f, 1f));
					return true;
				}
				return false;
			}
		};
		centerRectangle.setColor(0, 0, 0);
		//centerRectangle.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier(new RotationModifier(6, 0, 360), new SequenceEntityModifier(new ScaleModifier(3, 1, 1.5f), new ScaleModifier(3, 1.5f, 1)))));
		scene.getLastChild().attachChild(centerRectangle);
		scene.registerTouchArea(centerRectangle);

	}
	

	@Override
	public void onLoadComplete() {

	}


}
